<?php

ini_set('include_path', '../');

require_once("util/configuration_parametters.php");

require_once("model/article_ping/to/article_ping_to.php");
require_once("model/article_ping/dao/article_ping_dao_factory.php");

require_once("model/article/to/article_to.php");
require_once("model/article/dao/article_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");


class Article_ping_dao_test {

	private $dao;
	private $autor;
	private $article;
	
	public function __construct() {}
	
	public function before_test() {			
		$this->autor = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxx','password','email'));		
		$date = date("Y-m-d H:i:s");
		$this->article = (object) Article_dao_factory::singleton()->get_dao()->create(new Article_to(0, 'title', $this->autor->get_login(), $date, 'open', 0));
			
		$this->dao = Article_ping_dao_factory::singleton()->get_dao();
	}
	
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$article_ping_to = new Article_ping_to($this->article->get_number(), 
													$this->autor->get_login(), 'comment', 
													'positive', $date, 'open');
			
													
			//create
			$article_ping_to = (object) $this->dao->create($article_ping_to);	
			echo 'create article_ping: ' . $article_ping_to->to_string() . '<br/>';
			
			//create existent 
			try {
				$this->dao->create($article_ping_to);
			}
			catch (Exception $e) {
				echo 'Correct error to insert article_ping: ' . $article_ping_to->to_string() . '<br/>';
			}
			
			//exist inexistent 
			$this->dao->exist(9999, "yyyyyyyyyyyy");
			echo 'No exist article_category 9999, yyyyyyyyyyyy<br/>';
			
			//exist existent 
			$this->dao->exist($article_ping_to->get_to_article_number(), 
									$article_ping_to->get_from_autor_login());
			echo 'exist article_ping: ' . $article_ping_to->to_string() . '<br/>';

			//find inexistent 
			try {
				$this->dao->find_by_article_and_autor_login(9999, "yyyyyyyyyyyy");
			}
			catch (Exception $e) {
				echo 'Correct error to find article_ping 9999, yyyyyyyyyyyy<br/>';
			}
			
			//find existent 
			$aux_article_ping_to = $this->dao->find_by_article_and_autor_login($article_ping_to->get_to_article_number(), 
																		$article_ping_to->get_from_autor_login());	
				if ($article_ping_to->equals($aux_article_ping_to)) 
					echo 'found article_ping: ' . $article_ping_to->to_string() . '<br/>';
				else
					echo 'not equal article_ping inserted';
					
			//update 
			$article_ping_to->set_state('wait');
			$this->dao->update($article_ping_to);
			$aux_article_ping_to = $this->dao->find_by_article_and_autor_login($article_ping_to->get_to_article_number(), 
																		$article_ping_to->get_from_autor_login());	
			echo 'update article_ping: ' . $aux_article_ping_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove(9999, "yyyyyyyyyyyy");
			}
			catch (Exception $e) {
				echo 'Correct error to remove article_ping 9999, yyyyyyyyyyyy<br/>';
			}
			
			//remove existent 
			$this->dao->remove($aux_article_ping_to->get_to_article_number(), 
									$aux_article_ping_to->get_from_autor_login());
			echo 'remove article_ping: ' . $aux_article_ping_to->to_string() . '<br/>';
				
		}
		catch (Exception $e) {
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function after_test() {
		Article_dao_factory::singleton()->get_dao()->remove($this->article->get_number());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
	}
}

//test
$test = new Article_ping_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>